package com.small.act;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.commom.AsyncImageLoader;
import com.small.model.FavDiscountModel;
import com.small.model.ShopBaseInfoModel;
import com.smell.R;


//y优惠券详情
public class DiscountDetailActivity extends Activity {

	// private ListView list_view;

	private ImageButton btn_back;

	private Action action;
	private int result;

	private HashMap<String, Object> favDiscountMap = null;
	int intPage = 1;
	TextView tv, tv_cou_desc, tv_shop_name, tv_shop_address, tv_shop_distance;

	FavDiscountModel discountModel;
	ArrayList<ShopBaseInfoModel> shopList;
	private ImageView image_cou;
	private Button btn_fav;
	private LinearLayout btn_shoplist,btn_detail_descr;
	private RelativeLayout btn_cal;
	private String   telNum = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discountdetail_view);
		action = new Action(DiscountDetailActivity.this);
		findView();
		initData();
	}

	public void findView() {
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		btn_back.setOnClickListener(onClick);
		// list_view = (ListView) this.findViewById(R.id.mylook_list);
		tv = (TextView) this.findViewById(R.id.textView1);
		tv_cou_desc = (TextView) this.findViewById(R.id.tv_cou_desc);
		image_cou = (ImageView) this.findViewById(R.id.image_cou);
		tv_shop_name = (TextView) this.findViewById(R.id.shop_name);
		tv_shop_address = (TextView) this.findViewById(R.id.shop_address);
		tv_shop_distance = (TextView) this.findViewById(R.id. shop_distance);		
		btn_fav = (Button)this.findViewById(R.id.btn_fave);
		btn_shoplist = (LinearLayout)this.findViewById(R.id.view_shoplist);
		btn_shoplist.setOnClickListener(onClick);
		btn_detail_descr = (LinearLayout)this.findViewById(R.id.view_info);
		btn_detail_descr.setOnClickListener(onClick);
		btn_cal = (RelativeLayout)this.findViewById(R.id.btn_cal);//看到了吗，这个是那个时间事件
		btn_cal.setOnClickListener(onClick);//这里是监听
	}

	private void initData() {
		action.showProgressDialog("正在提交...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			// favDiscountMap =
			// action.getFavDiscountInfo(Common.model.getMemberID(), 2, 1);
			favDiscountMap = action.getDiscountDetail("1");
		}

		@Override
		public void onPrepare() {
		} 
		@Override
		public void onFinish() {

			action.stopProgressDialog();

			if (favDiscountMap != null) {
				Integer i = (Integer) favDiscountMap.get("result");
				result = (int) i;

				resultMethod(result, favDiscountMap);
			} else {
				resultMethod(-2, null);
			}
		}
	};

	OnClickListener onClick = new OnClickListener() {//鼠标指向一个方法，摁住ctrl可以跳进这个方法

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_back:// 返回
				finish();
				break;
			case R.id.view_shoplist:// 跳到商铺列表
				
				Intent i = new Intent(DiscountDetailActivity.this, ShopContactListActivity.class);
  
				Bundle mBundle = new Bundle();
				mBundle.putSerializable("shopList", shopList);
				i.putExtras(mBundle);
				startActivity(i);
				break;
			case R.id.view_info:// 图文详情

				break;
			case R.id.btn_cal:// 打电话  你把这个打电话功能实现了，懂？
				  Log.e("》》》》》",String.valueOf(telNum));
				if ( telNum!=null) { 
				   
					AlertDialog.Builder bulider = new Builder(DiscountDetailActivity.this);
					bulider.setTitle("提示");
					bulider.setMessage("确定打电话给"+telNum+"?");
					bulider.setNegativeButton("取消", null);
					bulider.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							 Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"18678831032"));
							   startActivity(callIntent);	
							
						}
					});
					bulider.show();
				   
				}else{
					 
				} 
				break;
			} 
		}
	};

	@SuppressWarnings("unchecked")
	protected void resultMethod(int result2,
			HashMap<String, Object> favDiscountMap2) {
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:

			String discount = favDiscountMap.get("resultStr").toString();
			tv.setText(discount);
			discountModel = (FavDiscountModel) favDiscountMap
					.get("discountModel");
			shopList = (ArrayList<ShopBaseInfoModel>) favDiscountMap
					.get("ShopList");

			if (favDiscountMap2 != null && shopList != null) {
				tv_cou_desc.setText(discountModel.getCouponDesc());
				AsyncImageLoader.getInstance().loadPre(	discountModel.getCouponID(),discountModel.getBigImageUrl(), image_cou);
				tv_shop_name .setText( shopList.get(0).getShopFirstName()+"("+shopList.get(0).getShopSecondName()+")");
				tv_shop_address .setText(shopList.get(0).getShopAddress());
				tv_shop_distance .setText(""+shopList.get(0).getShopLatitude());
				if (discountModel.getFavoriteTime()!=null&&!discountModel.getFavoriteTime().equals("")) {
					btn_fav.setText("收藏("+discountModel.getFavoriteTime()+"次)");
				}else{
					btn_fav.setText("收藏(0次)");
				}
				
				telNum = shopList.get(0).getShopTel();
				
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}
	}
}
